from rest_framework import serializers

from account.models import User
from article.models import Article


class ClassifySerialzier(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    classify_name = serializers.CharField(read_only=True)


class TagSerialzier(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    tag_name = serializers.CharField(read_only=True)


class GetArticleSerializer(serializers.Serializer):
    id = serializers.IntegerField(required=True)
    author = serializers.CharField(source='author.username', read_only=True)
    title = serializers.CharField(read_only=True)
    public = serializers.BooleanField(read_only=True)
    classifies = ClassifySerialzier(many=True, read_only=True)
    tags = TagSerialzier(many=True, read_only=True)
    publish_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', read_only=True)
    modify_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', read_only=True)
    content = serializers.CharField(read_only=True)


class DeleteArticleSerializer(serializers.Serializer):
    ids = serializers.ListField(required=True, child=serializers.IntegerField())


# class ListArticleSeralizer(serializers.Serializer):
#     user = serializers.CharField(required=False, write_only=True)
#     id = serializers.IntegerField(read_only=True)
#     author = serializers.CharField(read_only=True)
#     title = serializers.CharField(read_only=True)
#     classifies = serializers.CharField(read_only=True)
#     tags = serializers.CharField(read_only=True)
#     publish_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
#     modify_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    # content = serializers.CharField(read_only=True)


class ListArticleSeralizer(serializers.Serializer):
    user = serializers.CharField(required=False, write_only=True)
    id = serializers.IntegerField(read_only=True)
    author = serializers.CharField(source='author.username', read_only=True)
    title = serializers.CharField(read_only=True)
    classifies = serializers.SlugRelatedField(slug_field='classify_name', many=True, read_only=True)
    tags = serializers.SlugRelatedField(slug_field='tag_name', many=True, read_only=True)
    publish_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    modify_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    text_content = serializers.CharField(read_only=True)


class ListSelfArticleSeralizer(serializers.Serializer):
    title_snippet = serializers.CharField(write_only=True, required=False, default=None)
    id = serializers.IntegerField(read_only=True)
    author = serializers.CharField(source='author.username', read_only=True)
    title = serializers.CharField(read_only=True)
    classifies = serializers.SlugRelatedField(slug_field='classify_name', many=True, read_only=True)
    tags = serializers.SlugRelatedField(slug_field='tag_name', many=True, read_only=True)
    publish_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    modify_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    content = serializers.CharField(read_only=True)
    text_content = serializers.CharField(read_only=True)


class ListStaredUserArticleSeralizer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    author = serializers.CharField(source='author.username', read_only=True)
    title = serializers.CharField(read_only=True)
    classifies = serializers.CharField(read_only=True)
    tags = serializers.CharField(read_only=True)
    publish_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    modify_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S')
    text_content = serializers.CharField(read_only=True)
    # content = serializers.CharField(read_only=True)


class UploadArticleSerializer(serializers.ModelSerializer):
    modify_id = serializers.IntegerField(required=False)
    is_modify = serializers.BooleanField(required=True)
    classifies = serializers.CharField(required=False, default=None)
    tags = serializers.CharField(required=False, default=None)

    class Meta:
        model = Article
        exclude = ['id', 'author', 'publish_time', 'modify_time']
        # extra_kwargs = {
        #     'classifies': {'required': False},
        #     'tags': {'required': False}
        # }
        read_only_fields = []


class CommentArticleSerializer(serializers.Serializer):
    article_id = serializers.IntegerField(write_only=True)
    is_child = serializers.BooleanField(default=False, required=False)
    content = serializers.CharField(min_length=1)
    parent_id_id = serializers.IntegerField(write_only=True, required=False, default=None)
    reply_to_id = serializers.IntegerField(write_only=True, required=False, default=None)
    id = serializers.IntegerField(read_only=True)
    username = serializers.CharField(source='user.username', read_only=True)
    user_id = serializers.IntegerField(source='user.id', read_only=True)
    avatar_url = serializers.CharField(source='user.avatar_url.avatar_url', read_only=True)
    create_date = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', read_only=True)
    parent_id = serializers.IntegerField(source='parent_id.id', read_only=True)
    reply_to = serializers.CharField(source='reply_to.username', read_only=True, default=None)
    up_count = serializers.IntegerField(read_only=True)


class ListArticleCommentSerializer(serializers.Serializer):
    article_id = serializers.IntegerField(write_only=True)
    id = serializers.IntegerField(read_only=True)
    username = serializers.CharField(source='user.username', read_only=True)
    user_id = serializers.IntegerField(source='user.id', read_only=True)
    avatar_url = serializers.CharField(source='user.avatar_url.avatar_url', read_only=True)
    content = serializers.CharField(read_only=True)
    create_date = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', read_only=True)
    is_child = serializers.BooleanField(read_only=True)
    parent_id = serializers.IntegerField(source='parent_id.id', read_only=True)
    reply_to = serializers.CharField(source='reply_to.username', read_only=True, default=None)
    up_count = serializers.IntegerField(read_only=True)


class ListArticleChildCommentSerializer(serializers.Serializer):
    article_id = serializers.IntegerField(write_only=True)
    parent = serializers.IntegerField(write_only=True)
    id = serializers.IntegerField(read_only=True)
    username = serializers.CharField(source='user.username', read_only=True)
    user_id = serializers.IntegerField(source='user.id', read_only=True)
    avatar_url = serializers.CharField(source='user.avatar_url.avatar_url', read_only=True)
    content = serializers.CharField(read_only=True)
    create_date = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', read_only=True)
    is_child = serializers.BooleanField(read_only=True)
    parent_id = serializers.IntegerField(source='parent_id.id', read_only=True)
    reply_to = serializers.CharField(source='reply_to.username', read_only=True, default=None)
    up_count = serializers.IntegerField(read_only=True)


class ArticleCommentUpSerializer(serializers.Serializer):
    comment_id = serializers.IntegerField(min_value=1)


class UploadFileSeralizer(serializers.Serializer):
    name = serializers.CharField(required=False)
    file = serializers.FileField()
